import { Component, OnInit, Input, ElementRef, Renderer2 } from '@angular/core';
import { NavParams } from '@ionic/angular';
import { DataService } from '@services/data.service';
import { CacheService } from '@services/cache.service';
import { MyNoteService } from '@services/myNote.service';
import { AlarmEchartsService } from '@services/echarts/alarm.echarts.service';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'alarm-deal',
  templateUrl: 'alarm-deal.html',
  styleUrls: ['alarm-deal.scss']
})
export class AlarmDealPage implements OnInit {
  @Input() type;       // 判断是集团还是项目
  companyId;           // 集团ID
  projectId;           // 项目ID
  options;             // 告警处理图表参数
  AlarmDeal;           // 告警处理图表数据
  Res;                 // 及时响应率
  Rec;                 // 及时恢复率
  ChartsHide;          // 无数据时隐藏框隐藏
  AlarmDealRes = {     // 告警及时响应率数据
    asc: [],
    desc: []
  };
  AlarmDealRec = {      // 告警及时恢复率数据
    asc: [],
    desc: []
  };

  // 设置默认选择的时间
  ItemIndex = [0, 0, 0];
  // 设置默认不显示下拉列表
  TimeBoolean = [false, false, false];

  // 设置可切换的时间
  TimeItem = ['今日', '昨日', '本周', '上周'];

  // 设置默认显示的时间
  timeOver = [this.TimeItem[0], this.TimeItem[0], this.TimeItem[0]];

  constructor(
    private cache: CacheService,
    // private navParams: NavParams,
    private myNote: MyNoteService,
    private dataService: DataService,
    private chart: AlarmEchartsService,
    public el: ElementRef,
    public renderer: Renderer2,
    private activatedRoute: ActivatedRoute
  ) {
    this.activatedRoute.queryParams.subscribe(params => {
      this.companyId = params.companyId ? params.companyId
      : this.cache.currentCompany.companyId;
    this.projectId = params.projectId ? params.projectId
      : this.cache.currentProject.projectId;
    });
  }

  ngOnInit() {
    this.getData(this.type, null, 1);
  }

  // 告警处理日期切换
  ListShow = function (i) {
    // for (let m = 0; m < this.TimeBoolean.length; m++) {
    //   if (m === i) {
    //     this.TimeBoolean[m] = !this.TimeBoolean[m];
    //   } else {
    //     this.TimeBoolean[m] = false;
    //   }
    // }
    for (let m = 0; m < this.TimeBoolean.length; m++) {
      const obj = this.el.nativeElement.querySelector('.Day' + m);
      this.renderer.setStyle(obj, 'transition', 'height 0.5s');
      if (m === i) {
        this.TimeBoolean[m] = !this.TimeBoolean[m];
        if (this.TimeBoolean[m]) {
          this.renderer.setStyle(obj, 'height', '160px');
        } else {
          this.renderer.setStyle(obj, 'height', '0');
        }
      } else {
        this.TimeBoolean[m] = false;
        this.renderer.setStyle(obj, 'height', '0');
      }
    }
  };

  // 告警处理数据切换
  ItemTimeSelect(ele, index) {
    const obj = this.el.nativeElement.querySelector('.Day' + ele);
    this.renderer.setStyle(obj, 'height', '0');
    this.timeOver[ele] = this.TimeItem[index];
    this.TimeBoolean[ele] = !this.TimeBoolean[ele];
    this.ItemIndex[ele] = index;
    const TimeType = index + 1;
    this.getData(this.type, ele, TimeType);
  }

  getData(type, ele, TimeType) {
    switch (ele) {
      case 0:
        this.getAlarmDeal(type, this.companyId, this.projectId, TimeType);
        break;
      case 1:
        this.getAlarmDealRes(type, this.companyId, this.projectId, TimeType);
        break;
      case 2:
        this.getAlarmDealRec(type, this.companyId, this.projectId, TimeType);
        break;
      default:
        this.getAlarmDeal(type, this.companyId, this.projectId, TimeType);
        this.getAlarmDealRes(type, this.companyId, this.projectId, TimeType);
        this.getAlarmDealRec(type, this.companyId, this.projectId, TimeType);
        break;
    }
  }

  // 绘制告警处理图表
  getAlarmDealChart(data) {
    let ReportProACount;
    // 告警总数
    ReportProACount = parseInt(data.alarmingCount + data.recoveryCount, 10) ?
      (parseInt(data.alarmingCount + data.recoveryCount, 10)).toString() : '0';
    if (parseInt(ReportProACount, 10)) {
      this.ChartsHide = false;
      // 已响应
      const ReportProHCount = data.responseCount ? data.responseCount.toString() : '0';
      // 及时响应
      const ReportProRecCount = data.timelyRecoveryCount ? data.timelyRecoveryCount : 0;
      // 未及时响应
      const ReportProNRecCount = data.recoveryCount - data.timelyRecoveryCount ? data.recoveryCount - data.timelyRecoveryCount : 0;
      // 及时恢复
      const ReportProResCount = data.timelyResponseCount ? data.timelyResponseCount : 0;
      // 未及时恢复
      const ReportProNResCount = data.responseCount - data.timelyResponseCount ? data.responseCount - data.timelyResponseCount : 0;
      // 未响应
      const ReportProNHCount = ReportProACount - data.responseCount ? ReportProACount - data.responseCount : 0;
      return this.chart.showRepotDoublePie(ReportProHCount, ReportProACount, ReportProRecCount,
        ReportProResCount, ReportProNResCount, ReportProNHCount, ReportProNRecCount);
    } else {
      this.ChartsHide = true;
    }
  }

  /**
   * 获取集团或项目告警处理数据
   * @param type
   * @param projectId
   * @param TimeType
   */
  getAlarmDeal(type, companyId, projectId, TimeType) {
    if (type === 1) {
      this.dataService.getGroupAlarmDeal(companyId, TimeType).subscribe(res => {
        this.AlarmDeal = res.data;
        this.Res = Math.round(res.data.timelyResponseCount / res.data.responseCount * 100);
        this.Rec = Math.round(res.data.timelyRecoveryCount / (res.data.alarmingCount + res.data.recoveryCount) * 100);
        this.options = this.getAlarmDealChart(this.AlarmDeal);
      });
    } else {
      this.dataService.getAlarmDeal(projectId, TimeType).subscribe(res => {
        this.AlarmDeal = res.data;
        this.Res = Math.round(res.data.timelyResponseCount / res.data.responseCount * 100);
        this.Rec = Math.round(res.data.timelyRecoveryCount / (res.data.alarmingCount + res.data.recoveryCount) * 100);
        this.options = this.getAlarmDealChart(this.AlarmDeal);
      });
    }
  }

  /**
   * 获取集团或项目及时响应率数据
   * @param type
   * @param projectId
   * @param TimeType
   */
  getAlarmDealRes(type, companyId, projectId, TimeType) {
    if (type === 1) {
      this.dataService.getGroupAlarmDealRes(companyId, TimeType).subscribe(res => {
        if (res.data.data) {
          for (const m of res.data.data) {
            for (const n of m) {
              n.rate = Math.round(n.rate);
            }
          }
          this.AlarmDealRes.asc = res.data.data[0];
          this.AlarmDealRes.desc = res.data.data[1];
        }
      });
    } else {
      this.dataService.getAlarmDealRes(projectId, TimeType).subscribe(res => {
        if (res.data.data) {
          for (const m of res.data.data) {
            for (const n of m) {
              n.rate = Math.round(n.rate);
            }
          }
          this.AlarmDealRes.asc = res.data.data[0];
          this.AlarmDealRes.desc = res.data.data[1];
        }
      });
    }

  }

  /**
   * 获取集团或项目及时恢复率数据
   * @param type
   * @param projectId
   * @param TimeType
   */
  getAlarmDealRec(type, companyId, projectId, TimeType) {
    if (type === 1) {
      this.dataService.getGroupAlarmDealRec(companyId, TimeType).subscribe(res => {
        if (res.data.data) {
          for (const m of res.data.data) {
            for (const n of m) {
              n.rate = Math.round(n.rate);
            }
          }
          this.AlarmDealRec.asc = res.data.data[0];
          this.AlarmDealRec.desc = res.data.data[1];
        }
      });
    } else {
      this.dataService.getAlarmDealRec(projectId, TimeType).subscribe(res => {
        if (res.data.data) {
          for (const m of res.data.data) {
            for (const n of m) {
              n.rate = Math.round(n.rate);
            }
          }
          this.AlarmDealRec.asc = res.data.data[0];
          this.AlarmDealRec.desc = res.data.data[1];
        }
      });
    }
  }

}
